<template>
  <transition name="slide">
    <music-list :title="title" :songs="songs" :bgImage="bgImage" :rank="rank"></music-list>
  </transition>

</template>

<script type="text/ecmascript-6">
  import MusicList from 'components/music-list/music-list'
  import {mapGetters} from 'vuex'
  import {getMusicList} from 'api/rank'
  import {ERR_OK} from 'api/config'
  import {createSong,getSongVkey} from "../../common/js/song";



  export default {
    name: "top-list",
    components:{
      MusicList
    },
   computed:{
      title() {
        return this.topList.topTitle
      },
      bgImage() {
        if (this.songs.length) {
          return this.songs[0].image
        }
        return ''
      },
      ...mapGetters([
        'topList'
      ])
    },
    data(){
      return {
        songs: [],
        rank: true
      }
    },
    created(){
      this._getMusicList()
    },
    methods:{
      _getMusicList(){
        if (!this.topList.id) {
          this.$router.push('/rank')
          return
        }
        getMusicList(this.topList.id).then((res)=>{
          if (res.code===ERR_OK){
            console.log(res);
            this.songs = this._formatSongs(res.songlist)
          }
        })

      },
      //重组 res.data.list 数据,只拿需要的
      _formatSongs(list){
        let result = []
        list.forEach((item)=>{
          // 解构赋值-拿到item 下的 musicData 列表数据
          let {data} = item
          //------------- 更新的加上vkey
          getSongVkey(data.songmid).then((res) => {
            const vkey = res.data.items[0].vkey;
            if (data.songid && data.albummid) {
              result.push(createSong(data, vkey))
            }
          })

        })
        return result
      },
      _normalizeSongs(list){
        let ret = []
        list.forEach((item)=>{
          let {musicData} = item // 解构赋值
          if (musicData.songid&& musicData.albummid){
            ret.push(createSong(musicData))

          }
        })
        return ret
      }

    }




  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .slide-enter-active, .slide-leave-active
    transition: all 0.3s ease

  .slide-enter, .slide-leave-to
    transform: translate3d(100%, 0, 0)
</style>
